package com.example.xinlijiankangsys.controller;

import com.example.xinlijiankangsys.pojo.Result;
import com.example.xinlijiankangsys.pojo.User;
import com.example.xinlijiankangsys.service.QuestionnaireService;
import com.example.xinlijiankangsys.service.UserService;
import com.example.xinlijiankangsys.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private QuestionnaireService questionnaireService;
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String userId=user.getUserId();
        String password=user.getPassword();
        if(userId.length()==0||password.length()==0){
            System.out.println("参数空");
            return ReturnResultUtil.failure("参数缺失");
        }else{
            User res=userService.isUserExist(userId,password);
            if(res!=null){
                System.out.println("用户"+userId+"登录");
                return ReturnResultUtil.success(res);
            }else {
                System.out.println("账户密码不匹配");
                return ReturnResultUtil.failure("账户密码不匹配");
            }
        }
    }
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        boolean res=userService.insertUser(user);
        if(res){
            return ReturnResultUtil.success();
        }else return ReturnResultUtil.failure("该学号/工号已注册！");
    }

    @GetMapping("/isUserNeedWaring/{userId}")
    public Result isUserNeedWaring(@PathVariable String userId) {
        return ReturnResultUtil.success(questionnaireService.checkUserNeedWaring(userId));
    }
}
